library ieee;
 use ieee.std_logic_1164.all;
 use ieee.std_logic_unsigned.all;

-- 2-input Mutex model for RTL and GL simulations 
-- special treatment for concurrent inputs: --


-------------------------------------------------------------------------------
entity mutex is
-------------------------------------------------------------------------------
port( 

      -- MUTEX input i/f: --
      R1      : in  std_logic;
      R2      : in  std_logic;

      -- MUTEX output i/f: --
      G1      : out std_logic;
      G2      : out std_logic
);           
-------------------------------------------------------------------------------
end mutex ;
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
architecture mutex_arch of mutex is
-------------------------------------------------------------------------------
    signal  g1_int      :  std_logic := '0'; -- simulative initialization.
    signal  g2_int      :  std_logic := '0'; -- simulative initialization.
begin

G1 <= g1_int after 50 ps;
G2 <= g2_int after 50 ps;

mutex_proc: process(R1, R2, g1_int, g2_int)
begin
  if (g1_int='0' and g2_int='0') then -- no valid output, ready for arbitration.
   
   if (R1='1') then -- R1 has here a priority here over R2 (just in the simulation).
      g1_int <= '1'; 
   elsif (R2='1') then
      g2_int <= '1'; 
   end if;
   
  else -- there is a (one) granted output.
   if (g1_int='1' and R1='0') then -- g1 reset.
      g1_int <= '0';
   end if;

   if (g2_int='1' and R2='0') then -- g2 reset.
      g2_int <= '0';
   end if;
  end if;
end process;
 
-------------------------------------------------------------------------------
end mutex_arch;
-------------------------------------------------------------------------------                 

   
-------------------------------------------------------------------------------
configuration  mutex_cfg  of mutex is
-------------------------------------------------------------------------------
   for mutex_arch
   end for;
-------------------------------------------------------------------------------
end  mutex_cfg;              
-------------------------------------------------------------------------------
                 
